#!/usr/bin/env python
# Copyright (c) 2014 Eugene Frolov <efrolov@mirantis.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from rdb.old_app.core import storage


rn_storage = storage.RepairNoteStorage()


def get_dispatch_note_list(req, **kwargs):
    return rn_storage.get_dispatch_note_list(
        offset=kwargs['start'],
        limit=kwargs['limit'],
        query=kwargs['query']
    )


def get_serial_number_list(req, **kwargs):
    return rn_storage.get_serial_number_list(
        offset=kwargs['start'],
        limit=kwargs['limit'],
        query=kwargs['query']
    )


def get_customer_list(req, **kwargs):
    return rn_storage.get_customer_list(
        offset=kwargs['start'],
        limit=kwargs['limit'],
        query=kwargs['query']
    )


def get_customer_group_list(req, **kwargs):
    return rn_storage.get_customer_group_list()


class Customers(object):

    def __call__(self, req, **kwargs):
        if req.method == 'GET':
            return rn_storage.get_customers_list(
                offset=kwargs['start'],
                limit=kwargs['limit']
            )
        elif req.method == 'POST':
            return 'sdsd'
        else:
            raise Exception('Unknown method %s for collection' % req.method)

    def obj(self, req, **kwargs):
        import json
        if req.method == 'PUT':
            result = rn_storage.update_customer(**json.loads(req.read()))
        else:
            raise Exception('Unknown method %s for resource' % req.method)
        return result

    def __getattr__(self, name):
        try:
            int(name)
            return self.obj
        except:
            return super(Customers, self).__getattribute__(name)

    def post(self, req, **kwargs):
        return 'ok'


customers = Customers()


def get_destination_list(req, **kwargs):
    return rn_storage.get_destination_list(
        offset=kwargs['start'],
        limit=kwargs['limit'],
        query=kwargs['query']
    )


def get_model_list(req, **kwargs):
    return rn_storage.get_model_list(
        offset=kwargs['start'],
        limit=kwargs['limit'],
        query=kwargs['query']
    )


def get_report(req, **kwargs):
    reverse = lambda x, y, z: (z, y, x)
    return rn_storage.get_report(
        offset=kwargs['start'],
        limit=kwargs['limit'],
        from_date='%s-%s-%s' % reverse(*kwargs['from_date'].split('-')),
        to_date='%s-%s-%s' % reverse(*kwargs['to_date'].split('-')),
        dispatchnote=(kwargs['dispatchnote']
                      if 'dispatchnote' in kwargs else None),
        serial_number=(kwargs['serial_number']
                       if 'serial_number' in kwargs else None),
        customer=kwargs['customer'] if 'customer' in kwargs else None,
        model=kwargs['model'] if 'model' in kwargs else None,
        destination=kwargs['destination'] if 'destination' in kwargs else None
    )


def get_xls(req, **kwargs):
    reverse = lambda x, y, z: (z, y, x)
    return rn_storage.get_xls(
        from_date='%s-%s-%s' % reverse(*kwargs['from_date'].split('-')),
        to_date='%s-%s-%s' % reverse(*kwargs['to_date'].split('-')),
        dispatchnote=(kwargs['dispatchnote']
                      if 'dispatchnote' in kwargs else None),
        serial_number=(kwargs['serial_number']
                       if 'serial_number' in kwargs else None),
        customer=kwargs['customer'] if 'customer' in kwargs else None,
        model=kwargs['model'] if 'model' in kwargs else None,
        destination=kwargs['destination'] if 'destination' in kwargs else None
    )


def get_report2(req, **kwargs):
    reverse = lambda x, y, z: (z, y, x)
    return rn_storage.get_report2(
        offset=kwargs['start'],
        limit=kwargs['limit'],
        from_date='%s-%s-%s' % reverse(*kwargs['from_date'].split('-')),
        to_date='%s-%s-%s' % reverse(*kwargs['to_date'].split('-')),
        dispatchnote=(kwargs['dispatchnote']
                      if 'dispatchnote' in kwargs else None),
        serial_number=(kwargs['serial_number']
                       if 'serial_number' in kwargs else None),
        customer=kwargs['customer'] if 'customer' in kwargs else None,
        model=kwargs['model'] if 'model' in kwargs else None,
        destination=kwargs['destination'] if 'destination' in kwargs else None
    )


def get_xls2(req, **kwargs):
    reverse = lambda x, y, z: (z, y, x)
    return rn_storage.get_xls2(
        from_date='%s-%s-%s' % reverse(*kwargs['from_date'].split('-')),
        to_date='%s-%s-%s' % reverse(*kwargs['to_date'].split('-')),
        dispatchnote=(kwargs['dispatchnote']
                      if 'dispatchnote' in kwargs else None),
        serial_number=(kwargs['serial_number']
                       if 'serial_number' in kwargs else None),
        customer=kwargs['customer'] if 'customer' in kwargs else None,
        model=kwargs['model'] if 'model' in kwargs else None,
        destination=kwargs['destination'] if 'destination' in kwargs else None
    )


def get_customers_xls(req, **kwargs):
    return rn_storage.get_customers_xls()
